﻿// Xshell Command - Set Registry String
// 
// <copyright file="SetRegistrySzCommand.cs" company="TaskyMedia LLC">
//   Copyright © 2010 TaskyMedia LLC
// </copyright>

namespace Xshell.Commands
{
    using System;

    /// <summary>
    /// Sets the value of a REG_SZ registry key pair.
    /// </summary>
    [XsCommand("SetRegistrySz", typeof(SetRegistrySzCommandArgs))]
    internal class SetRegistrySzCommand : IXsCommand
    {
        /// <summary>
        /// Executes the command with the specified arguments.
        /// </summary>
        /// <param name="args">The command arguments.</param>
        public void ExecuteCommand(IXsCommandArgs args)
        {
            // Validate the arguments
            if (args == null)
            {
                throw new InvalidOperationException("Command arguments are required for this operation");
            }

            if (!(args is SetRegistrySzCommandArgs))
            {
                throw new InvalidOperationException("Invalid command argument container type");
            }

            SetRegistrySzCommandArgs registryArgs = (SetRegistrySzCommandArgs)args;
            if (String.IsNullOrEmpty(registryArgs.Key))
            {
                throw new InvalidOperationException("Key is a required argument");
            }

            if (String.IsNullOrEmpty(registryArgs.Name))
            {
                throw new InvalidOperationException("Name is a required argument");
            }

            if (String.IsNullOrEmpty(registryArgs.Value))
            {
                throw new InvalidOperationException("Value is a required argument");
            }

            Microsoft.Win32.Registry.SetValue(registryArgs.Key, registryArgs.Name, registryArgs.Value, Microsoft.Win32.RegistryValueKind.String);
        }
    }
}
